package com.xiaoshuidi.cloud.module.iot.controller.admin.report;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.iot.controller.admin.report.vo.WaterElectricityReportReq;
import com.xiaoshuidi.cloud.module.iot.controller.admin.report.vo.WaterElectricityReportResp;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.iot.enums.RedisConstants;
import com.xiaoshuidi.cloud.module.iot.service.CacheService;
import com.xiaoshuidi.cloud.module.iot.service.WaterElectricityReportService;
import com.xiaoshuidi.cloud.module.iot.vo.BaseExportResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 水电明细报表
 *
 * @Author Feng
 * @date 2023/9/22
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping(ApiConstants.NAME + "/report")
public class WaterElectricityReportController {

    private final WaterElectricityReportService waterElectricityReportService;
    private final CacheService cacheService;

    /**
     * 水电明细报表 - 列表
     *
     * @param req
     * @return
     */
    @PostMapping("/page")
    public CommonResult<PageResult<WaterElectricityReportResp>> page(@RequestBody WaterElectricityReportReq req) {
        log.info("水电明细报表请求参数： {}", JsonUtils.toJsonString(req));
        return CommonResult.success(waterElectricityReportService.page(req));
    }

    /**
     * 根据条件导出明细
     *
     * @param req
     * @return
     */
    @PostMapping("/export")
    public CommonResult<String> export(@RequestBody WaterElectricityReportReq req) {
        try {
            log.info("水电报表明细导出，请求参数： {}", JsonUtils.toJsonString(req));
            String key = UUID.randomUUID().toString();
            String redisKey = String.format(RedisConstants.WaterElectricityReportExport, key);
            BaseExportResp resp = new BaseExportResp();
            resp.setCode(100);
            cacheService.putObject(redisKey, resp, 2000);
            waterElectricityReportService.export(req, key);
            return CommonResult.success(key);
        } catch (Exception e) {
            log.error("水电报表明细导出： {}", e);
            return CommonResult.error(ErrorCodeConstants.REPORT_EXPORT_ERROR);
        }
    }

    /**
     * 获取导出地址
     *
     * @param key
     * @return
     */
    @GetMapping("/getDownloadUrl")
    public CommonResult<?> getDownloadUrl(@RequestParam("key") String key) {

        try {
            Object o = cacheService.getObject(String.format(RedisConstants.WaterElectricityReportExport, key));
            if (o == null) {
                log.error("水电报表明细导出失败");
                return CommonResult.error(ErrorCodeConstants.REPORT_EXPORT_ERROR);
            }
            return CommonResult.success(o);
        } catch (Exception e) {
            log.error("水电报表明细导出： {}", e);
            return CommonResult.error(ErrorCodeConstants.REPORT_EXPORT_ERROR);
        }
    }
}
